package com.chinacoal.hr.levelimpl.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinacoal.hr.levelapi.entity.Organization;
import com.chinacoal.hr.levelimpl.mapper.OrganizationMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-29 22:58
 * @description:
 **/
@Component
public class OrganizationDao {

    @Resource
    private OrganizationMapper organizationMapper;

    /**
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.Organization>
     * @author : YunTao.Li
     * @date : 2019/11/29 2019/11/29
     */
    public List<Organization> getOrganizationList(Map<String, Object> condition) throws Throwable {
        String isLeaf = condition.get("isLeaf") != null ? condition.get("isLeaf").toString() : "";
        String parentOrgId = condition.get("parentOrgId") != null ? condition.get("parentOrgId").toString() : "";
        String orgLevel = condition.get("orgLevel") != null ? condition.get("orgLevel").toString() : "";
        String orgLevelThen = condition.get("orgLevelThen") != null ? condition.get("orgLevelThen").toString() : "";


        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(isLeaf)) {
            queryWrapper.eq("ISLEAF", isLeaf);
        }

        if (StringUtils.isNotBlank(orgLevel)) {
            queryWrapper.eq("ORGLEVEL", orgLevel);
        }

        if (StringUtils.isNotBlank(orgLevelThen)) {
            queryWrapper.ge("ORGLEVEL", orgLevelThen);
        }

        if (StringUtils.isNotBlank(parentOrgId)) {
//            queryWrapper.eq("PARENTORGID",parentOrgId);
            if (("0").equals(parentOrgId)) {
                parentOrgId = "";
            }
            String rootOrgId = "1527";
            String rootPath = ".1527";
            if (rootOrgId.equals(parentOrgId)) {
                rootPath = "";
            }
            queryWrapper.likeRight("ORGSEQ", rootPath + "%." + parentOrgId);
        }

        queryWrapper.orderByAsc("ORGSEQ");

        List<Organization> organizations = organizationMapper.selectList(queryWrapper);

        return organizations;
    }

    /**
     * selected by id
     *
     * @param orgId :
     * @return : com.chinacoal.hr.levelimpl.entity.Organization
     * @author : YunTao.Li
     * @date : 2020/1/7 2020/1/7
     */
    public Organization getOrganization(String orgId) throws Throwable {
        return this.organizationMapper.selectById(orgId);
    }

    /**
     * selected by id
     *
     * @param condition :
     * @return : com.chinacoal.hr.levelimpl.entity.Organization
     * @author : YunTao.Li
     * @date : 2020/1/7 2020/1/7
     */
    public Organization getOrganization(Map condition) throws Throwable {
        String orgCode = condition.get("orgCode") != null ? condition.get("orgCode").toString() : "";

        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(orgCode)) {
            queryWrapper.eq("ORGCODE", orgCode);
        }

        queryWrapper.last("limit 1");

        return this.organizationMapper.selectOne(queryWrapper);
    }
}
